// pages/news/news.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    count: 0,
    pageList: [],
    showList: [],
    contents: '',
  },

  gotoExtern(e) {
    // console.log(e.currentTarget.dataset.contents)
    console.log(e.currentTarget.dataset.title)

    wx.navigateTo({
      url: '/pages/extern/extern?contents=' + encodeURIComponent(e.currentTarget.dataset.contents)
                + '&title=' + encodeURIComponent(e.currentTarget.dataset.title)
                + '&url=' + encodeURIComponent(e.currentTarget.dataset.url),
    })
    // console.log(e)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("loading")
    this.getPageList()
  },

  getPageList() {
    wx.request({
      url: 'https://epidemic-notification.alisssqaq.cn/news',
      method: 'GET',
      success: (res) => {
        var dt = JSON.parse(res.data)
        console.log(dt)
        this.setData({
          pageList: dt,
          count: 0
        })
        this.getShowList()
      }
    })
  },

  getShowList() {
    let cnt = this.data.count
    let list = this.data.pageList
    let slist = this.data.showList
    this.setData({
      showList: cnt + 6 > list.length ?
        slist.concat(list.slice(cnt, list.length)) : slist.concat(list.slice(cnt, cnt + 6)),
      count: cnt + 6
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log("refresh and request")
    this.getPageList()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log("reach bottom")
    wx.showLoading({
      title: '更多新闻加载..',
    })
    this.getShowList()
    wx.hideLoading()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})